package com.sgb.kjwl.view.ui.adapter.self.packers;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.self.SelfTeamDeviceEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

public class SelfTeamDeviceAdapter extends BaseRecycleAdapter<SelfTeamDeviceEntity> {

    private Button delView;
    private Button editView;
    private ItemCallBack itemCallBack;
    private LinearLayout deviceDesLay,deviceNumLay,deviceModelLay;

    public SelfTeamDeviceAdapter(Context context, List<SelfTeamDeviceEntity> datas) {
        super(context, R.layout.item_self_team_device, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, final SelfTeamDeviceEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        editView = holder.getItem().findViewById(R.id.btn_edit);
        deviceModelLay = holder.getItem().findViewById(R.id.device_model_layout);
        deviceNumLay = holder.getItem().findViewById(R.id.device_num_layout);
        deviceDesLay = holder.getItem().findViewById(R.id.device_des_layout);
        // editView.setVisibility(View.GONE);
        editView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemCallBack != null) {
                    itemCallBack.edit(entity.getNo());
                }
            }
        });

        delView = holder.getItem().findViewById(R.id.btn_delete);
        delView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemCallBack != null) {
                    itemCallBack.del(entity.getNo());
                }
            }
        });

       ImageView imgLogo= holder.itemView.findViewById(R.id.img_logo);
        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.packer_avatar)
                .transforms(new CenterCrop(), new RoundedCorners(8));
        if (!TextUtils.isEmpty(entity.getPicture())){
            String[] split = entity.getPicture().split(",");
            Glide.with(context).load(split[0]).apply(options).into(imgLogo);
        }else {
            Glide.with(context).load(R.mipmap.mechanical).apply(options).into(imgLogo);
        }
        if (TextUtils.isEmpty(entity.getCountNum())){
            deviceNumLay.setVisibility(View.GONE);
        }else {
            deviceNumLay.setVisibility(View.VISIBLE);
        }
        if (TextUtils.isEmpty(entity.getDescribe())){
            deviceDesLay.setVisibility(View.GONE);
        }else {
            deviceDesLay.setVisibility(View.VISIBLE);
        }
        if (TextUtils.isEmpty(entity.getModel())){
            deviceModelLay.setVisibility(View.GONE);
        }else {
            deviceModelLay.setVisibility(View.VISIBLE);
        }
    }

    public void setItemCallBack(ItemCallBack itemCallBack) {
        this.itemCallBack = itemCallBack;
    }

    public interface ItemCallBack {
        void del(String id);

        void edit(String id);
    }
}
